<template lang="jade">
  el-dialog(:visible="toggleActiveAccount",title="激活账号",:before-close="doCancel")
    el-form(:model="account",ref="input")
      el-row(:gutter="20")
        el-col(:span="5")
          el-form-item
            span.lbl-project.pull-left 保证金金额：
        el-col(:span="14")
          el-form-item(prop="deposit",:rules="[{ required: true, type: 'number' ,message: '请输入保证金', trigger: 'blur' },{ type: 'number', message: '请输入正确的保证金', trigger: 'blur,change' }]")
            el-input(placeholder="请输入保证金金额",v-model.trim.number="account.deposit")
        el-col(:span="4")
          span.lbl-project.pull-left (万元)
      el-row(:gutter="20")
        el-col(:span="24")
          el-button(type="primary",style="width:100px;height:35px",:loading="loading.status",@click="doConfirm") {{loading.text}}
          el-button(style="width:100px;height:35px",@click="doCancel") 取消
</template>

<script>
  import utilsApi from '@/api/utils'
  import {mapGetters} from 'vuex'
  export default {
    name: 'ActiveAccountDialog',
    computed: {
      ...mapGetters(['toggleActiveAccount'])
    },
    props: {
      corporation: {
        type: Object
      }
    },
    data: () => ({
      loading: {
        text: '激活账号',
        status: false
      },
      account: {
        deposit: ''
      }
    }),
    methods: {
      doConfirm () {
        this.$confirm('确定要激活账号吗？', '提示', {
          dangerouslyUseHTMLString: true,
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading.text = '激活中...'
          this.loading.status = true
          utilsApi.authCorporation({
            status: 1,
            companyId: this.corporation.id,
            deposit: this.account.deposit,
            type: 'activate'
          }).then(res => {
            if (res.data.errcode === 0) {
              this.$message({
                type: 'success',
                message: '账号激活成功!'
              })
              this.$store.commit('TOGGLE_ACTIVE_DIALOG')
              this.$emit('sync-data')
            } else {
              this.$message({
                type: 'error',
                message: res.data.errmsg
              })
            }
            this.doReset()
          }, err => {
            this.$message({
              type: 'error',
              message: err.response.data.message
            })
            this.doReset()
          })
        })
      },
      doCancel () {
        this.$confirm('确定要取消吗？', '提示', {
          dangerouslyUseHTMLString: true,
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.doReset()
          this.$store.commit('TOGGLE_ACTIVE_DIALOG')
        })
      },
      doReset () {
        this.loading.text = '激活账号'
        this.loading.status = false
        this.$refs.input.resetFields()
      }
    }
  }
</script>

<style lang="scss">
  @import "../../assets/css/mixin";
</style>
